//
//  ByUINLoginVC.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUINLoginVC.h"

// vc
#import "ByUINRegisterVC.h"     // 注册
#import "ByUIPswLoginVC.h"
#import "ByUIMobleLoginVC.h"

// view
#import "ByViewNLoginSegment.h"

@interface ByUINLoginVC ()<UIScrollViewDelegate>
{
    NSInteger _curIndex;
}
@property (nonatomic, strong) ByViewNLoginSegment *segment;
@property (nonatomic, strong) UIScrollView *svContent;
@property (nonatomic, strong) NSArray *titleAry;
@end

@implementation ByUINLoginVC
#pragma mark getter
- (ByViewNLoginSegment *)segment {
    if (!_segment) {
        _segment = [[ByViewNLoginSegment alloc] init];
    }
    return _segment;
}

- (NSArray *)titleAry {
    if (!_titleAry) {
        _titleAry = [NSArray arrayWithObjects:@"账号密码登录",@"手机号快捷登录", nil];
    }
    return _titleAry;
}


- (UIScrollView *)svContent {
    if (!_svContent) {
        _svContent = [[UIScrollView alloc] init];
        _svContent.pagingEnabled = YES;
        _svContent.showsHorizontalScrollIndicator = NO;
        _svContent.delegate = self;
    }
    return _svContent;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    [self addLeftBackBarButtonItem];
    [self addBYItemviews:@[@"注册"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    [self creatSegmentView];
}

#pragma amrk 创建ui
- (void)creatSegmentView {
    
    self.segment.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
    self.segment.titlesAry = self.titleAry;
    __weak typeof(self) weakSelf = self;
    [self.segment setIndexChangeBlock:^(NSInteger index) {
        [weakSelf indexChange:index];
        _curIndex = index;
    }];
    [self.view addSubview:self.segment];
    // 内容滑动视图
    self.svContent.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 64  - 40);
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH * (self.titleAry.count), 0);
    [self.view addSubview:self.svContent];
    [self indexChange:0];

}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger index = scrollView.contentOffset.x / pageWidth;
    if (index == _curIndex) {
        return;
    }
    _curIndex = index;
    [_segment setIndex:index];
//    [self addViewController:index];
}

- (void)indexChange:(NSInteger)index {
    [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH * index, 0) animated:NO];
    [self addViewController:index];
}

- (void)addViewController:(NSInteger)index {
    
    
    if (index == 0) {
        NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
        for (UIView *view in self.svContent.subviews) {
            if ([view.viewController isKindOfClass:[ByUIPswLoginVC class]]) {
                [maryIndex addObject:[NSNumber numberWithInteger:view.tag]];
            }
        }
        if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]){
            ByUIPswLoginVC *vc = [[ByUIPswLoginVC alloc] init];
            vc.seletedTag = _seletedTag;
            [self addChildViewController:vc];
            vc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
            [self.svContent addSubview:vc.view];
            vc.view.tag = index;
        }
    }else if (index == 1){
        NSMutableArray *maryIndex = [[NSMutableArray alloc]init];
        for (UIView *view in self.svContent.subviews) {
            if ([view.viewController isKindOfClass:[ByUIMobleLoginVC class]]) {
                [maryIndex addObject:[NSNumber numberWithInteger:view.tag]];
            }
        }
        if (![maryIndex containsObject:[NSNumber numberWithInteger:index]]){
            ByUIMobleLoginVC *vc = [[ByUIMobleLoginVC alloc] init];
            vc.seletedTag = _seletedTag;
            [self addChildViewController:vc];
            vc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, self.svContent.frame.size.width, self.svContent.frame.size.height);
            [self.svContent addSubview:vc.view];
            vc.view.tag = index;
        }
        
    }
    for (UIView *view in self.svContent.subviews) {
        for (UIView *subView in view.subviews) {
            if ([subView isKindOfClass:[UITextField class]] ) {
                [(UITextField *)subView resignFirstResponder];
            }
        }
        
    }
}


#pragma mark 点击事件
- (void)rightItemsAction:(NSInteger)rightIndex {
    ByUINRegisterVC *vc = [[ByUINRegisterVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
